package com.whfc.openapi.api.hdc.controller;

import com.whfc.common.enums.NetState;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.hdc.dto.FshDeviceConfigDTO;
import com.whfc.hdc.dto.FshTencentCloudUserSignDTO;
import com.whfc.hdc.dto.FshWarnRuleDTO;
import com.whfc.hdc.dto.HdcAccessTokenDTO;
import com.whfc.hdc.params.FshDeviceConfigParam;
import com.whfc.hdc.params.FshDeviceDataParam;
import com.whfc.hdc.params.FshEnterRoomParam;
import com.whfc.hdc.params.HdcOpenApiTokenParam;
import com.whfc.openapi.constant.OpenApiConst;
import com.whfc.openapi.api.hdc.service.HdcFshOpenService;
import com.whfc.openapi.api.sys.service.OpenApiTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 智能安全帽开放接口
 *
 * @author qzexing
 * @version 1.0
 * @date 2021-04-06 14:32
 */
@RestController
@RequestMapping("/open/api/v1/fsh")
public class HdcFshOpenController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HdcFshOpenService hdcFshOpenService;

    @Autowired
    private OpenApiTokenService openApiTokenService;


    @PostMapping("/login")
    public Result login(@RequestBody HdcOpenApiTokenParam param) {
        logger.info("智能安全帽开机, param:[{}]", param);
        HdcAccessTokenDTO tokenDTO = openApiTokenService.getHdcOpenApiToken(param);
        if (tokenDTO != null) {
            hdcFshOpenService.updateNetState(tokenDTO.getDeviceId(), tokenDTO.getSn(), NetState.ONLINE.getValue());
        }
        return ResultUtil.success(tokenDTO);
    }

    @PostMapping("/logout")
    public Result logout(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO) {
        logger.info("智能安全帽关机, sn:{}", clientDTO.getSn());
        hdcFshOpenService.updateNetState(clientDTO.getDeviceId(), clientDTO.getSn(), NetState.OFFLINE.getValue());
        return ResultUtil.success();
    }


    @GetMapping("/config")
    public Result config(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO) {
        logger.info("智能安全帽获取配置, sn:{}", clientDTO.getSn());
        FshDeviceConfigDTO config = hdcFshOpenService.getConfig(clientDTO.getSn());
        return ResultUtil.success(config);
    }


    @PostMapping("/config")
    public Result configSet(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO,
                            @RequestBody FshDeviceConfigParam param) {
        logger.info("智能安全帽设置配置, sn:{}, params:{}", clientDTO.getSn(), param);
        param.setDeviceId(clientDTO.getDeviceId());
        param.setSn(clientDTO.getSn());
        hdcFshOpenService.setConfig(param);
        return ResultUtil.success();
    }


    @PostMapping("/current-data")
    public Result currentData(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO,
                              @RequestBody FshDeviceDataParam param) {
        logger.info("智能安全帽上报数据, sn:{}, params:{}", clientDTO.getSn(), param);
        param.setDeviceId(clientDTO.getDeviceId());
        param.setSn(clientDTO.getSn());
        hdcFshOpenService.addCurrentData(param);
        return ResultUtil.success();
    }

    @PostMapping("/media")
    public Result media(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO,
                        @RequestParam("type") Integer type,
                        @RequestParam MultipartFile file) {
        logger.info("智能安全帽上报多媒体数据, sn:{}, type:{}, file:{}", clientDTO.getSn(), type, file.getOriginalFilename());
        hdcFshOpenService.saveMedia(clientDTO.getDeviceId(), clientDTO.getSn(), type, file);
        return ResultUtil.success();
    }

    @PostMapping("/enter-room")
    public Result enterRoom(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO,
                            @RequestBody FshEnterRoomParam param) {
        logger.info("智能安全帽发起通话, sn:{}, params:{}", clientDTO.getSn(), param);
        hdcFshOpenService.enterRoom(clientDTO.getSn(), param);
        return ResultUtil.success();
    }


    @GetMapping("/tencentcloud/userSig")
    public Result userSig(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO) {
        logger.info("智能安全帽获取腾讯云用户签名, sn:{}", clientDTO.getSn());
        FshTencentCloudUserSignDTO userSign = hdcFshOpenService.getTencentCloudUserSign(clientDTO.getSn());
        return ResultUtil.success(userSign);
    }

    @PostMapping("/push/register")
    public Result pushRegister(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO,
                               @RequestParam("registrationId") String registrationId) {
        logger.info("智能安全帽上传极光注册ID, sn:{}, registrationId:{}", clientDTO.getSn(), registrationId);
        hdcFshOpenService.pushRegister(clientDTO.getDeviceId(), clientDTO.getSn(), registrationId);
        return ResultUtil.success();
    }

    @GetMapping("/warn/type")
    public Result warnType(@RequestAttribute(OpenApiConst.OPENAPI_HDC_CLIENT) HdcAccessTokenDTO clientDTO) {
        logger.info("智能安全帽获取报警类型, sn:{}", clientDTO.getSn());
        List<FshWarnRuleDTO> list = hdcFshOpenService.getWarnTypeList();
        return ResultUtil.success(new ListData<>(list));
    }


}
